public with sharing class AccountsEXT 
{
	transient public List<Account> accounts,accountsSize;
	transient public string filters {get;set;}
	public string pageNumbers;	
	public string whereClause = '';
	public string queryString = 'Select Id, Name, billingStreet, billingCity, billingState, RecordType.isPersonType From account ';
	public string queryEnd = ' ';
	public string searchTerm;
	public boolean isPerson {get;set;}
	public string currentUser;
	
	
	
	//Pagination Variables
   	private integer counter=0;  //keeps track of the offset
   	private integer list_size=250; //sets the page size or number of rows
   	transient public integer total_size; //used to show user the total size of the list		   	
   	
   	public string getpageNumbers()
   	{
   		Long pageCounter;

   		decimal totalPagesDec = (decimal.valueOf(total_size)/250);
   		long tpgs = (total_size/250);
   		integer totalPages = integer.valueOf(totalPagesDec.round(system.roundingMode.Up));
   		
   		if(counter == 0)
   		{
   			pageCounter = 1; 
   		}
   		else
   		{
   			pageCounter = decimal.valueOf((counter/250)).round(system.roundingMode.Up) + 1;
   		}  		   		
   		
   		getAccounts();
   		pageNumbers = 'Page ' + string.valueOf(pageCounter) + ' of ' + string.valueOf(totalPages);   		
   		return pageNumbers;
   	}     	

	public accountsEXT(ApexPages.StandardController controller)
	{
		//total_size = [select count() from Account Limit 250]; //set  the total size in the constructor	
		currentUser = system.Userinfo.getUserId();
		filters = 'My accounts';
	} 
	
	public PageReference newAccount()
   	{
     	return new PageReference('/agent/apex/vfNewAccount');
	}		
	
	public PageReference newClient()
   	{
     	return new PageReference('/agent/apex/vfNewPersonAccount');  
	}	
	
	public List<SelectOption> getFilter()
	{
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('My Accounts', 'My Accounts'));
		options.add(new SelectOption('Insurance Companies', 'Insurance Companies'));
		options.add(new SelectOption('Insurance Agencies', 'Insurance Agencies'));
		options.add(new SelectOption('Clients', 'Clients'));
		options.add(new SelectOption('New This Week', 'New This Week'));
				
		return options;
	}
	
	public String getFilters()
	{
		return filters;
	}
	
	public void setFilters(String filters)
	{
		this.filters = filters;
	}
	
	public Boolean getisPerson()
	{
		return isPerson;
	}
	
	public void setisPerson(Boolean isPerson)
	{
		this.isPerson = isPerson;
	}	
	
	public void doSearch()
	{
		filters = 'Custom Search';
		getAccounts();
	}	
	
	public string getsearchTerm()
	{
		return searchTerm;
	}
	
	public void setsearchTerm(string sTerm)
	{
		searchTerm = sTerm;
	}
	
	//Pagination 
   public Account [] getaccounts() 
   {
      total_size = 1;
      
      try {	      	
      	
		if(filters == 'Insurance Agencies')
		{
			whereClause = 'Where RecordType.Name = \'Agency\'';
		}
		else if(filters == 'Insurance Companies')
		{
			whereClause = 'Where RecordType.Name = \'Insurance Company\'';
		}		
		else if(filters == 'New This Week')
		{
			whereClause = 'Where CreatedDate = This_Week';
		}
		else if(filters == 'Clients')
		{
			whereClause = 'Where RecordType.Name = \'Client\'';
		}
		else if(filters == 'Custom Search')
		{
			whereClause = 'Where Name like \'%' +  searchTerm + '%\'';
		}
			
		queryEnd = 'Order by Name Limit ' + list_size + ' offset ' + counter;
						
		if(filters == 'My accounts')
		{
			string Owner = ' Where OwnerId = \'' + currentUser + '\' ';
			accounts = database.query(queryString + Owner + queryEnd);
			string qString = queryString + Owner + ' Limit 250';
			accountsSize = database.query(qString );
			total_size  = accountsSize.size();
			
		}
		else
		{
			accounts = database.query(queryString + whereClause + queryEnd);
			string qString = queryString + whereClause + ' Limit 250';
			accountsSize = database.query(qString);
			total_size = accountsSize.size();		
				
		}		
		
		return accounts;	
		      	
      }
		catch (QueryException e) 
		{
	        ApexPages.addMessages(e);   
	        return null;
      	}
   }     
       
   public PageReference Beginning() { //user clicked beginning
      counter = 0;
      getPageNumbers();
      return null;
   }
 
   public PageReference Previous() { //user clicked previous button
      counter -= list_size;
      getPageNumbers();
      return null;
   }
 
   public PageReference Next() { //user clicked next button
      counter += list_size;
      getPageNumbers();
      return null;
   }
 
   public PageReference End() { //user clicked end
      counter = total_size - math.mod(total_size, list_size);
      getPageNumbers();
      return null;
   }
 
   public Boolean getDisablePrevious() { 
      //this will disable the previous and beginning buttons
      if (counter>0) return false; else return true;
   }
 
   public Boolean getDisableNext() { //this will disable the next and end buttons
      if (counter + list_size < total_size) return false; else return true;
   }
 
   public Integer getTotal_size() {
      return total_size;
   }
 
   public Integer getPageNumber() {
      return counter/list_size + 1;
   }
 
   public Integer getTotalPages() {
      if (math.mod(total_size, list_size) > 0) {
         return total_size/list_size + 1;
      } else {
         return (total_size/list_size);
      }
   }	       
}